package tq.com.tqteacher.model;

import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TQFileInfo {
    private String date;
    private String name;
    private String type;
    private String tag;
    private String cover;
    private String id;


    static public TQFileInfo fromJson(JSONObject pObject){
        if(pObject == null) return null;

        TQFileInfo lInfo = new TQFileInfo();
        lInfo.date = pObject.optString("datetime");
        lInfo.name = pObject.optString("name");
        lInfo.type = pObject.optString("type");
        lInfo.tag = pObject.optString("tag");
        lInfo.cover = pObject.optString("cover");
        lInfo.id = pObject.optString("id");

        if(lInfo.date != null){
            SimpleDateFormat lDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date lDate = lDateFormat.parse(lInfo.date);
                SimpleDateFormat lSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                lInfo.date = lSimpleDateFormat.format(lDate);

            }catch (ParseException pE){
                pE.printStackTrace();
            }
        }
        return lInfo;
    }


    public String fileId(){
        return id;
    }
    public String getDate() {
        return date;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public String getTag() {
        return tag;
    }

    public String getCover() {
        return cover;
    }


    public boolean isMp4(){
        return getType().compareToIgnoreCase("mp4") == 0;
    }
    public boolean isPdf(){
        return getType().compareToIgnoreCase("pdf") == 0;
    }
    public boolean isPng(){
        return getType().compareToIgnoreCase("png") == 0;
    }
}
